import {
	getToken,
	setToken,
	removeToken,
	getUserId,
	setUserId,
	removeUserId
} from '@/common/localStoarge.js'

const state = {
	token: getToken() || null,
	userId: getUserId() || null,
	userInfo: null
}
const mutations = {
	SET_TOKEN(state, token) {
		setToken(token)
		state.token = token
	},
	REMOVE_TOKEN(state) {
		removeToken()
		state.token = null
	},
	SET_USER_ID(state, id) {
		setUserId(id)
		state.userId = id
	},
	REMOVE_USER_ID(state) {
		removeUserId()
		state.userId = null
	},
	SET_USER_INFO(state, data) {
		state.userInfo = data
	},
	REMOVE_USER_INFO(state) {
		state.userInfo = null
	},
}
const actions = {
	login(store, { token, id }) {
		if (token) store.commit('SET_TOKEN', token)
		if (id) store.commit('SET_USER_ID', id)
	},
	logout(store) {
		store.commit('REMOVE_TOKEN')
		store.commit('REMOVE_USER_ID')
		store.commit('REMOVE_USER_INFO')
	}
}

export default {
	state,
	mutations,
	actions,
	namespaced: true
}
